package com.lm.bouncycastle.controller;

import com.lm.bouncycastle.utils.AesUtil;
import com.lm.bouncycastle.utils.HashUtil;
import org.springframework.web.bind.annotation.*;
import javax.crypto.SecretKey;

@RestController
@RequestMapping("/crypto")
public class CryptoController {

    private final SecretKey aesKey;

    public CryptoController() throws Exception {
        this.aesKey = AesUtil.generateKey();
    }

    @GetMapping("/aes/encrypt")
    public String aesEncrypt(@RequestParam String data) throws Exception {
        return AesUtil.encrypt(data, aesKey);
    }

    @GetMapping("/aes/decrypt")
    public String aesDecrypt(@RequestParam String cipher) throws Exception {
        return AesUtil.decrypt(cipher, aesKey);
    }

    @GetMapping("/hash/sha256")
    public String sha256(@RequestParam String data) throws Exception {
        return HashUtil.sha256(data);
    }
}
